package com.sm.healthy.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.sm.healthy.R
import com.sm.healthy.base.BaseFragment
import com.sm.healthy.base.handle.TopHandle
import com.sm.healthy.databinding.FragmentHistoryDataBinding
import com.sm.healthy.model.HistoryDataViewModel
import com.sm.healthy.ui.fragment.historyfrag.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_history_data.*
import kotlinx.android.synthetic.main.fragment_home.*
import me.hgj.jetpackmvvm.ext.nav

// TODO: Rename parameter arguments, choose names that match

/**
 * A simple [Fragment] subclass.
 * Use the [HistoryDataFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class HistoryDataFragment : BaseFragment<HistoryDataViewModel, FragmentHistoryDataBinding>() {


    private val historyBloodFlag by lazy(LazyThreadSafetyMode.NONE) {
        HistoryBloodFlag()
    }

    private val historySpo2Flag by lazy(LazyThreadSafetyMode.NONE) {
        HistorySpo2Flag()
    }

    private val historyOxygenFlag by lazy(LazyThreadSafetyMode.NONE) {
        HistoryOxygenFlag()
    }

    private val historyEcgFlag by lazy(LazyThreadSafetyMode.NONE) {
        HistoryEcgFlag()
    }

    private val historyFetalFlag by lazy(LazyThreadSafetyMode.NONE) {
        HistoryFetalFlag()
    }

    private val historyTemperatureFlag by lazy(LazyThreadSafetyMode.NONE) {
        HistoryTemperatureFlag()
    }

    private lateinit var curFrg: BaseFragment<*, *>
    override fun layoutId(): Int {
        return R.layout.fragment_history_data
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.handle = TopHandle(nav())

        childFragmentManager.beginTransaction()
            .add(R.id.frag_container, historyTemperatureFlag)
            .hide(historyTemperatureFlag)
            .add(R.id.frag_container, historyFetalFlag)
            .hide(historyFetalFlag)
            .add(R.id.frag_container, historyEcgFlag)
            .hide(historyEcgFlag)
            .add(R.id.frag_container, historyOxygenFlag)
            .hide(historyOxygenFlag)
            .add(R.id.frag_container, historySpo2Flag)
            .hide(historySpo2Flag)
            .add(R.id.frag_container, historyBloodFlag)
            .commit()

        curFrg = historyBloodFlag
        mDatabind.rgMenu.setOnCheckedChangeListener { group, checkedId ->
            val beginTransaction = childFragmentManager.beginTransaction()
            beginTransaction.hide(curFrg)
            when (checkedId) {
                R.id.rb_blood_press -> {
                    beginTransaction
                        .show(historyBloodFlag).commit()
                    curFrg = historyBloodFlag
                }

                R.id.rb_oxygen -> {
                    beginTransaction
                        .show(historySpo2Flag).commit()
                    curFrg = historySpo2Flag
                }

                R.id.rb_sugar -> {
                    beginTransaction
                        .show(historyOxygenFlag).commit()
                    curFrg = historyOxygenFlag
                }
                R.id.rb_ecg -> {
                    beginTransaction
                        .show(historyEcgFlag).commit()
                    curFrg = historyEcgFlag
                }

                R.id.rb_fetal_heart_rate -> {
                    beginTransaction
                        .show(historyFetalFlag).commit()
                    curFrg = historyFetalFlag
                }

                R.id.rb_temperature -> {
                    beginTransaction
                        .show(historyTemperatureFlag).commit()
                    curFrg = historyTemperatureFlag
                }
            }
        }
    }

}